/*
 * code.c
 *
 *  Created on: Mar 26, 2022
 *      Author: Abyss
 */
#include "main.h"
#include "config.h"
#include "core_delay.h"
#include "String.h"

#define Refresh_NH4  "t8.txt=\"%.1fPPM\""
#define Refresh_PH   "t9.txt=\"%.1f\""
#define Refresh_temp "t10.txt=\"%.1f\""
#define Refresh_DO   "t11.txt=\"%.1f%%\""

int this_id = 10001;
double PH,DO,NH4,temp;

uint8_t tmp_buf[1024];
char SendBuff[100];

/*轮询扫描3路ADC 分别对应PH 溶氧 氨氮 */

//123

void get_value()
{
	double Votal[3];

	for(int i = 0;i < 1000;i++)
		for(int i = 0;i< 3;i++)
			Votal[i] += get_adc(hadc1);

	HAL_ADC_Stop(&hadc1);
	for(int i = 0;i< 3;i++)
		Votal[i] = Votal[i]*3.3/4096000;

	PH = -5.7541*Votal[0] + 16.654;    //计算PH值·
	DO =Votal[1]* 55.555;  //计算溶氧率
	NH4 = Votal[2]*4.51; //计算NH4浓度
	temp = Ds18b20_Get_Temp();

}

void USART_HMI_Send() //0-NH4 1-PH 2-Temp 3-DO
{
	sprintf(SendBuff,Refresh_NH4,NH4);
	int lenth = strlen(SendBuff);
	for(int i = 1;i <=3 ;i++)
		SendBuff[lenth++] = 0xFF;
	HAL_UART_Transmit(&huart2, SendBuff, lenth, 100);

	sprintf(SendBuff,Refresh_PH,PH);
	lenth = strlen(SendBuff);
	for(int i = 1;i <=3 ;i++)
		SendBuff[lenth++] = 0xFF;
	HAL_UART_Transmit(&huart2, SendBuff, lenth, 100);

	sprintf(SendBuff,Refresh_temp,temp);
	lenth = strlen(SendBuff);
	for(int i = 1;i <=3 ;i++)
		SendBuff[lenth++] = 0xFF;

	HAL_UART_Transmit(&huart2, SendBuff, lenth, 100);

	sprintf(SendBuff,Refresh_DO,DO);
	lenth = strlen(SendBuff);
	for(int i = 1;i <=3 ;i++)
		SendBuff[lenth++] = 0xFF;

	HAL_UART_Transmit(&huart2, SendBuff, lenth, 100);

}


void run()
{
	modbus_init();
	Ds18b20_Init();
	while(1)
	{
		get_value();
		USART_HMI_Send();
		/*串口屏数据刷新*/
	}
}
